﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.DBUtilities;
using ShopB2C.DBUtilities.SqlDbHelper;
using ShopB2C.Entity;
using ShopB2C.Enums;
using ShopB2C.Interfaces;
using ShopB2C.ViewModels;

namespace ShopB2C.SQLDataAccess {
    public class Consult : IConsult {

        #region Const Variables

        private const string _consultUpdate = "Consult_Update";
        private const string _consultDelete = "Consult_Delete";
        private const string _consultSelect = "Consult_Select";
        private const string _consultSelectList = "Consult_SelectList";
        private const string _consultProduct = "Consult_Product";

        #endregion

        public Consult(string connectionString) {
            DbBase.ConnectionString = connectionString;
        }

        #region Consult

        public void ConsultUpdate(ConsultEntity ce) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@ConsultId", ce.ConsultId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Content", 1000, ce.Content).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<DateTime>("@AddDate", ce.AddDate).SqlDbType = SqlDbType.DateTime;
            sqlParams.Add<Boolean>("@IsAudit", ce.IsAudit).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<String>("@ReplyContent", 1000, ce.ReplyContent).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<DateTime>("@ReplyDate", ce.ReplyDate).SqlDbType = SqlDbType.DateTime;
            sqlParams.Add<Int32>("@ProductId", ce.ProductId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@Uid", ce.Uid).SqlDbType = SqlDbType.Int;
            SqlDbProcedure.ExecuteNonQuery(_consultUpdate, sqlParams.ToArray);
        }

        public void ConsultDelete(string consultIds) {
            SqlDbProcedure.ExecuteNonQuery(_consultDelete,
                DbParams<SqlParameter>.CreateParameter<string>("@ConsultIds", consultIds));
        }

        public ConsultEntity GetConsultById(int consultId) {
            ConsultEntity ce = null;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_consultSelect,
                DbParams<SqlParameter>.CreateParameter<int>("@ConsultId", consultId));
            if (dr.HasRows) {
                if (dr.Read()) {
                    ce = new ConsultEntity() {
                        ConsultId = (int)dr["ConsultId"],
                        Content = dr["Content"].ToString(),
                        AddDate = (DateTime)dr["AddDate"],
                        IsAudit = (Boolean)dr["IsAudit"],
                        ReplyContent = dr["ReplyContent"].ToString(),
                        ReplyDate = dr["ReplyDate"] is DBNull ? DateTime.MinValue : (DateTime)dr["ReplyDate"],
                        ProductId = (int)dr["ProductId"],
                        Uid = (int)dr["Uid"]
                    };
                }
            }
            dr.Close();
            dr.Dispose();
            return ce;
        }

        public List<ConsultEntity> GetConsultList(ConsultParameters cp) {
            List<ConsultEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@PageSize", cp.PageSize).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageIndex", cp.PageIndex).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@ProductId", cp.ProductId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<bool?>("@IsAudit", cp.IsAudit).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<Int32>("@Uid", cp.Uid).SqlDbType = SqlDbType.Int;
            sqlParams.Add<string>("@Key", 50, cp.Key).SqlDbType = SqlDbType.NVarChar;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_consultSelectList, sqlParams.ToArray);
            if (dr.HasRows) {
                listResult = new List<ConsultEntity>();
                while (dr.Read()) {
                    cp.PageCount = (int)dr["PageCount"];
                    cp.RowsCount = (int)dr["RowsCount"];
                    listResult.Add(new ConsultEntity() {
                        ConsultId = (int)dr["ConsultId"],
                        Content = dr["Content"].ToString(),
                        AddDate = (DateTime)dr["AddDate"],
                        IsAudit = (Boolean)dr["IsAudit"],
                        ReplyContent = dr["ReplyContent"].ToString(),
                        ReplyDate = dr["ReplyDate"] is DBNull ? DateTime.MinValue : (DateTime)dr["ReplyDate"],
                        ProductId = (int)dr["ProductId"],
                        Uid = (int)dr["Uid"],
                        UserName = dr["UserName"].ToString(),
                        NickName = dr["NickName"].ToString(),
                        LevelId = dr["LevelId"] is DBNull ? 0 : (int)dr["LevelId"],
                        LevelName = dr["LevelName"].ToString(),
                        LevelIcon = dr["LevelIcon"].ToString()
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public List<ProductEntity> GetConsultProduct(bool? audit, int pageSize, int pageIndex, out int pageCount) {
            pageCount = 0;
            List<ProductEntity> listResult = new List<ProductEntity>();

            DbParams<SqlParameter> parameters = new DbParams<SqlParameter>();
            parameters.Add<int>("@PageSize", pageSize).SqlDbType = SqlDbType.Int;
            parameters.Add<int>("@PageIndex", pageIndex).SqlDbType = SqlDbType.Int;
            parameters.Add<bool?>("@IsAudit", audit).SqlDbType = SqlDbType.Int;

            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_consultProduct, parameters.ToArray);
            if (dr.HasRows) {
                while (dr.Read()) {
                    pageCount = (int)dr["PageCount"];
                    listResult.Add(new ProductEntity() {
                        ProductId = (int)dr["ProductId"],
                        ProductName = dr["ProductName"].ToString(),
                        SubName = dr["SubName"].ToString(),
                        Description = dr["Description"].ToString(),
                        KeyWords = dr["KeyWords"].ToString(),
                        Price = (decimal)dr["Price"],
                        ClickCount = (int)dr["ClickCount"],
                        ReplyCount = (int)dr["ReplyCount"],
                        GradeValue = (decimal)dr["GradeValue"],
                        StockCount = (int)dr["StockCount"],
                        SalesCount = (int)dr["SalesCount"],
                        AddDateTime = (DateTime)dr["AddDateTime"],
                        IsActive = (bool)dr["IsActive"],
                        SaleDateTime = (DateTime)dr["SaleDateTime"],
                        DisCount = (double)dr["DisCount"],
                        ProductNo = dr["ProductNo"].ToString(),
                        ClassId = (int)dr["ClassId"],
                        ImageName = dr["ImageName"].ToString(),
                        BrandId = (int)dr["BrandId"],
                        Status = (ProductStatus)dr["Status"],
                        UnAuditConsultNum = (int)dr["UnAuditConsultNum"]
                    });
                }
            }
            dr.Close();
            dr.Dispose();

            return listResult;
        }

        #endregion

    }
}
